package com.int_tech.timeTask194;

import com.int_tech.MessageCodeAll.dao.CodeA0Dao;
import com.int_tech.MessageCodeAll.dao.CodeA0NewDao;
import com.int_tech.MessageCodeAll.entity.CodeA0;
import com.int_tech.MessageCodeAll.entity.CodeA0New;
import com.int_tech.utils.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/7/14.
 */
@Component
public class CheckCodeA0ToNewA0 {

    @Autowired
    private CodeA0Dao codeA0Dao;

    @Autowired
    private CodeA0NewDao codeA0NewDao;

    @Scheduled(cron="0/59 * * * * ?")
    public void checkA0() throws ParseException {

        //每次遍历100条
        PageRequest pageRequest = new PageRequest(0, 2000);
        Page<CodeA0> codeA0Page = codeA0Dao.findByCheckStatus(pageRequest,0);
//        Page<CodeA0> codeA0Page = codeA0Dao.findByCheckStatusAndCarNumber(pageRequest,0,"020T11580");
        List<CodeA0> codeA0List = codeA0Page.getContent();

        for(CodeA0 codeA0 : codeA0List) {
            try {
                codeA0.setCheckStatus(1);
                codeA0Dao.save(codeA0);

                /*Date date = DateTool.string2Date("2018-08-05 00:00:00","yyyy-MM-dd HH:mm:ss");
                if(codeA0.getCreatedTime().before(date))
                {
                    continue;
                }*/

                //这个过滤简单，直接判断新表里面有没有数据一模一样的，如果有，则不添加
                CodeA0New codeA0New = codeA0NewDao.findByDataString(codeA0.getDataString());

                //如果为空，添加一条
                if (codeA0New == null) {
                    codeA0New = new CodeA0New();

                    codeA0New.setCarNumber(codeA0.getCarNumber());
                    if(codeA0.getDataString().startsWith("A01"))
                    {
                        codeA0New.setCheckStatus(2);
                    }
                    else
                    {
                        codeA0New.setCheckStatus(0);
                    }

                    codeA0New.setCreatedTime(codeA0.getCreatedTime());
                    codeA0New.setDataString(codeA0.getDataString());
                    codeA0New.setHang(codeA0.getHang());
                    codeA0New.setLie(codeA0.getLie());
                    codeA0New.setHeight(codeA0.getHeight());
                    codeA0New.setLat(codeA0.getLat());
                    codeA0New.setLng(codeA0.getLng());
                    codeA0New.setLength(codeA0.getLength());
                    codeA0New.setRate(codeA0.getRate());
                    codeA0New.setRateDateString(codeA0.getRateDateString());
                    codeA0New.setWidth(codeA0.getWidth());

                    String rateString = codeA0.getRateDateString();

                    String rs[] = rateString.split(",");

                    int m = 0;

                    //2个（含2个）以下探头有数据，并大于100，小于1000，A0其他数据小于100，短信报警，加入报警新表
                    for(int i = 0;i < rs.length; i++)
                    {
                        if(Integer.parseInt(rs[i]) > 100)
                        {
                            m++;
                        }

                        if(Integer.parseInt(rs[i]) > 1000)
                        {
                            m = 4;
                        }
                    }

                    if(m < 3)
                    {
                        //探头有超过两个小于100的
                        codeA0.setCheckStatus(-2);
                        codeA0Dao.save(codeA0);
                        codeA0New.setRate("0");
                        codeA0New.setStatus(0);
                        codeA0NewDao.save(codeA0New);
                    }
                    else
                    {
                        codeA0New.setStatus(0);
                        codeA0NewDao.save(codeA0New);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                codeA0.setCheckStatus(-1);
                codeA0Dao.save(codeA0);
            }
        }
    }
}
